package org.codefinger.dao.callback;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.codefinger.dao.impl.ResultSetCallback;
import org.codefinger.dao.util.jdbc.JDBCUtil;

public class MapQueryCallback implements ResultSetCallback<Map<String, Object>> {

	public static final MapQueryCallback	INSTANCE	= new MapQueryCallback();

	private MapQueryCallback() {
		super();
	}

	@Override
	public Map<String, Object> getResult(ResultSet resultSet, String[] aliases) throws SQLException {
		ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
		if (resultSet.next()) {
			int i = 1;
			Map<String, Object> map = new HashMap<String, Object>();
			for (String alias : aliases) {
				map.put(alias, JDBCUtil.getResult(//
						resultSet, i, resultSetMetaData.getColumnType(i++)));
			}
			return map;
		}
		return null;
	}

}
